var checkAll = document.getElementsByClassName("check-all")[0];

var checkOneList = document.getElementsByClassName("check-one");
var priceList = document.getElementsByClassName("price");
var addList = document.getElementsByClassName("add");
var subtotalList = document.getElementsByClassName("subtotal");
var deleteList = document.getElementsByClassName("delete");
var countList = document.getElementsByClassName("count");
var countInpList = document.getElementsByClassName("count-input");
var reduceList = document.getElementsByClassName("reduce");
var TrList = document.getElementsByClassName("tr");
var catbox = document.getElementsByClassName('catbox')[0];
var clearAll = document.getElementById("clearAll");
var deleteAll = document.getElementById("deleteAll");


//单选影响全选
/* for (let i = 0; i < checkOneList.length; i++) {
    var checkOne = checkOneList[i];
    checkOne.onclick = function () {
        var flag = true;
        for (var j = 0; j < checkOneList.length; j++) {
            var item = checkOneList[j];
            if (!item.checked) {
                flag = false;
                break;
            }
        }
        checkAll.checked = flag;
    }
} */


catbox.onclick = function (e) {
    var e = e || window.event;
    var target = e.target || e.srcElement;

    //全选控制单选
    if (target.classList.contains("check-all")) {
        var status = target.checked;
        // checkOneList.checked = status;
        for (var i = 0; i < checkOneList.length; i++) {
            var checkOne = checkOneList[i];
            checkOne.checked = status;
        }
    }

    //单选影响全选
    if (target.classList.contains("check-one")) {
        for (let i = 0; i < checkOneList.length; i++) {
            var checkOne = checkOneList[i];
            checkOne.onclick = function () {
                var flag = true;
                for (var j = 0; j < checkOneList.length; j++) {
                    var item = checkOneList[j];
                    if (!item.checked) {
                        flag = false;
                        break;
                    }
                }
                checkAll.checked = flag;
            }
        }
    }

    //实现数量的加
    if (target.className == "add") {
        var index = Array.from(addList).indexOf(target);
        var num = countInpList[index].value;
        num++;
        countInpList[index].value = num;

        //减号出现
        reduceList[index].textContent = "-";


        // 小计更新  = 单价 * 数量
        var cost = priceList[index].textContent;
        subtotalList[index].textContent = cost * num;

    }
    // 实现数量的减
    if (target.className == "reduce") {
        var index = Array.from(reduceList).indexOf(target);
        var num = countInpList[index].value;
        //输入框  数量 减一 （细节要求  数量为1的时候  不再减）

        //如果数量为一，则不能减;
        if (num == 1) return false;
        num--;
        //如果减完之后数量为一，那么减号要去掉
        if (num == 1) reduceList[index].textContent = "";//当if后面只有一行代码时，可以省略花括号

        countInpList[index].value = num;

        //小计更新  =  单价*数量//找到父元素的后一个元素：总价

        var dj = priceList[index].textContent;
        subtotalList[index].textContent = dj * num;
    }

    //实现删除功能
    //删除单行
    if (target.className == "delete") {
        var index = Array.from(deleteList).indexOf(target);
        if (confirm("是否删除该商品")) {
            TrList[index].remove();
        }
    }

    if (target.id == "deleteAll") {
        console.log(target);
        for (var i = 0; i < checkOneList.length; i++) {
            var checkOne = checkOneList[i];
            if (checkOne.checked) {
                var parentTr = parent(parent(checkOne));
                parentTr.remove();
                i--;
            }
        }

        isAllchecked();
    }
}
    function isAllchecked() {
        var flag = true; // 假设都选中 => 如果存在未选中的 推翻假设
        for (var j = 0; j < checkOneList.length; j++) {
            var item = checkOneList[j];
            if (!item.checked) {  // item.checked ==false
                flag = false;
                break;
            }
        }
        checkAll.checked = checkOneList.length == 0 ? false : flag;
    }